import Mock from 'mockjs'

// 模拟用户数据
let users = [
  {
    username: 'admin',
    password: '123456',
    token: 'admin-token',
    email: 'admin@example.com'
  },
  {
    username: 'user',
    password: '123456',
    token: 'user-token',
    email: 'user@example.com'
  }
]

// 登录接口
Mock.mock('http://127.0.0.1/login', 'get', (options) => {
  const { username, password } = JSON.parse(options.body || '{}')
  const user = users.find(
    (user) => user.username === username && user.password === password
  )

  if (user) {
    return {
      code: 200,
      message: '登录成功',
      data: {
        token: user.token,
        userInfo: {
          username: user.username,
          email: user.email
        }
      }
    }
  }
  return {
    code: 401,
    message: '用户名或密码错误'
  }
})

// 注册接口
Mock.mock('http://127.0.0.1/register', 'post', (options) => {
  const { username, password, email } = JSON.parse(options.body || '{}')
  
  if (users.some(user => user.username === username)) {
    return {
      code: 400,
      message: '用户名已存在'
    }
  }

  const newUser = {
    username,
    password,
    email,
    token: `${username}-token-${Date.now()}`
  }
  
  users.push(newUser)
  
  return {
    code: 200,
    message: '注册成功',
    data: {
      username: newUser.username
    }
  }
})

// 输出mock配置，方便调试
console.log('Mock Initialized') 